/**
 * 项目设置 列表页
 *
 * 作者 ganjun
 * 日期 2024-11-12 10:58:48
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'dropdown', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        layer = layui.layer,
        table = layui.table,
        dropdown = layui.dropdown,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-sypqProject'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#sypqProject-table-form'),
        //  $delete = $view.find('#delete'),
        $approveStatus = $view.find('#approveStatus'),
        $remarkid = $view.find('#remarkid'),
        $remark = $view.find('#remark'),
        $status = $view.find('#status'),
        tableIns;

    form.verify(validate);
    form.render();
    febs.initDictItemSelect($approveStatus, 'APPROVE_STATUS', null, null);
    febs.initDictItemSelect($status, 'PROJECT_STATUS', null, null);
    initTable();


    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'sypqProjectTable',
            url: ctx + 'sypqProject/list',
            where: getQueryParams(),
            height: getTableHeight(),
            autoSort: true,
            skin: '',
            cols: [[
                {field: 'createdTime', title: '', width: 100,sort:true,hide:true,order:'desc'},
                {field: 'projectCode', title: '项目编码', width: 130, templet:function (d){
                    return '<div class="layui-table-cell-overflow">'+d.projectCode+'</div>';
                    }},
                {field: 'projectName', title: '项目名称', width: 200, sort: true},
                {field: 'managers', title: '项目经理', width: 200, sort: true},
                {field: 'auditor', title: '项目审批人', width: 80},
                {field: 'custName', title: '客户单位', width: 180},
                {field: 'approveStatus', title: '审核状态', width: 50, hide: true},
                {field: 'remark', title: '审核意见', width: 10, hide: true},
                {field: 'approveStatusName', title: '审核状态', width: 40},
                {field: 'statusName', title: '项目状态', width: 40},
                {field: 'expectStartdate', title: '期望开始时间', width: 150},
                {field: 'expectEnddate', title: '期望结束时间', width: 150},
                {field: 'planStartdate', title: '计划开始时间', width: 150},
                {field: 'planEnddate', title: '计划结束时间', width: 150},
                {field: 'actualStartdate', title: '实际开始时间', width: 120},
                {field: 'actualEnddate', title: '实际结束时间', width: 120},
                {title: '操作', width: 180, align: 'left', templet: function (d) {
                        var str = ' <div class="layui-clear-space">';
                        if (d.approveStatus == 1||d.approveStatus == 2 || d.approveStatus == 4) {
                            str += '<a lay-event="shipoModel" shiro:hasPermission="modelShip:view">' +
                                '<em class="layui-icon febs-edit-area febs-blue">&#xe7a7;</em>船模</a>';
                            str += '<a lay-event="oarsModel" shiro:hasPermission="modelOars:view">' +
                                '<em class="layui-icon febs-edit-area febs-green">&#xe7b0;</em>桨模</a>';
                        }else if(d.approveStatus == 3){
                            str += '<a lay-event="edit" shiro:hasPermission="sypqProject:edit">' +
                                '<em class="layui-icon febs-edit-area febs-cyan">&#xe83f;</em>修改</a>';
                            str += '<a lay-event="copy" shiro:hasPermission="sypqProject:copy">' +
                                '<em class="layui-icon febs-edit-area febs-bg-lightblue">&#xe8a9;</em>复制</a>';
                        }
                        str += '<a class="layui-btn layui-btn-xs" lay-event="more">' +
                            '更多<i class="layui-icon layui-icon-down"></i></a></div>';
                        return str;

                    }
                },
            ]],
            defaultToolbar: [],
            toolbar: '#projectToolbar',
            done: function (res, curr, count) {
                // 表格渲染完毕后的回调,为表格中的行绑定点击事件
                $('body').on('click', '.layui-table-body tr', function () {
                    // 获取当前行的数据
                    var data = table.cache['sypqProjectTable'][this.dataset.index];
                    // 执行行点击后的操作
                    if (data.approveStatus == 3) {
                        console.log('审核意见：', data.remark);
                        $remarkid.css("display", "block");
                        $remark.val(data.remark);
                    } else {
                        $remarkid.css("display", "none");
                        $remark.val("");
                    }
                });
            }
        });
    }

    table.on('sort(sypqProjectTable)', function (obj) {
        tableIns.reload({
            initSort: obj,
            where: $.extend(getQueryParams(), {
                field: obj.field,
                order: obj.type
            }),
            autoSort: true
        });
    });

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    table.on('tool(sypqProjectTable)', function (obj) {
        var data = obj.data,
            layEvent = obj.event;
        if (layEvent === 'shipoModel') {
            parent.layui.febs.navigate("/modelShip?projectName=" + data.projectName, true);
        }

        if (layEvent === 'oarsModel') {
            parent.layui.febs.navigate("/modelOars?projectName=" + data.projectName, true);
        }

        if (layEvent === 'edit') {
            parent.layui.febs.navigate('/sypqProject/update/' + data.projectCode, true);
        }
        if (layEvent === 'copy') {
            parent.layui.febs.navigate('/sypqProject/copy/' + data.projectCode, true);
        }

        if (layEvent === 'more') {
            // 更多 - 下拉菜单
            button = [];
            if (data.approveStatus == 1 ||data.approveStatus == 2 || data.approveStatus == 4) {
                var attachModel = {
                    id: 'attachModel',
                    title: '附体',
                    templet: '<a><em class="layui-icon  febs-edit-area  layui-bg-gray">&#xe7fb;</em>附体</a>'
                }
                if(febs.include(permissionSet,"modelAttach:view")){
                    button.push(attachModel);
                }
            }

            if (data.approveStatus == 1 ||data.approveStatus == 2 || data.approveStatus == 4) {
                var task = {
                    id: 'task',
                    title: '任务',
                    templet: '<a><em class="layui-icon febs-edit-area' +
                        ' febs-purple">&#xe7a4;</em>任务</a>'
                }
                if(febs.include(permissionSet,"task:view")){
                    button.push(task);
                }
            }

            if (data.approveStatus == 1 ||data.approveStatus == 2 || data.approveStatus == 4) {
                var schedule = {
                    id: 'schedule',
                    title: '排期',
                    templet: '<a><em class="layui-icon   febs-edit-area' +
                        ' febs-orange">&#xe8c7;</em>排期</a>'
                }
                if(febs.include(permissionSet,"schedule:view")){
                    button.push(schedule);
                }
            }

            if (data.approveStatus == 4) {
                var progress = {
                    id: 'progress',
                    title: '进度',
                    templet: '<a><em class="layui-icon febs-edit-area  febs-geekblue">&#xe7a5;</em>进度</a>'
                }
                if(febs.include(permissionSet,"progress:view")){
                    button.push(progress);
                }
            }

            if ((data.approveStatus == 1)) {
                var edit = {
                    id: 'edit',
                    title: '修改',
                    templet: '<a><em class="layui-icon febs-edit-area febs-cyan">&#xe83f;</em>修改</a>'
                }
                if(febs.include(permissionSet,"sypqProject:edit")){
                    button.push(edit);
                }
            }

            if (data.approveStatus == 2) {
                var audit = {
                    id: 'audit',
                    title: '审批',
                    templet: '<a><em class="layui-icon febs-edit-area  febs-bg-green-lime">&#xe7f1;</em>审批</a>'
                }
                if(febs.include(permissionSet,"sypqProject:audit")){
                    button.push(audit);
                }
            }
            if (data.approveStatus == 1||data.approveStatus == 2||data.approveStatus == 4) {
                var copy = {
                    id: 'copy',
                    title: '复制',
                    templet: '<a><em class="layui-icon  febs-edit-area febs-bg-lightblue">&#xe8a9;</em>复制</a>'
                }
                if(febs.include(permissionSet,"sypqProject:copy")){
                    button.push(copy);
                }
            }


            var detail = {
                id: 'detail',
                title: '详情',
                templet: '<a><em class="layui-icon febs-edit-area  febs-bg-orange">&#xe7b3;</em>详情</a>'
            }
            if(febs.include(permissionSet,"sypqProject:detail")){
                button.push(detail);
            }

            if (data.approveStatus != 4) {
                var del = {
                    id: 'del',
                    title: '删除',
                    templet: '<a><em class="layui-icon febs-edit-area  febs-bg-gold">&#xe7f9;</em>删除</a>'
                }
                if(febs.include(permissionSet,"sypqProject:delete")){
                    button.push(del);
                }
            }

            if ((data.approveStatus == 4) && (data.status == 6)) {
                var stop = {
                    id: 'stop',
                    title: '终止',
                    templet: '<a><em class="layui-icon febs-edit-area febs-tag-geekblue">&#xe89a;</em>终止</a>'
                }
                if(febs.include(permissionSet,"sypqProject:stop")){
                    button.push(stop);
                }
            }

            dropdown.render({
                elem: this, // 触发事件的 DOM 对象
                show: true, // 外部事件触发即显示
                data: button,
                click: function (menudata) {
                    if (menudata.id === 'attachModel') {
                        parent.layui.febs.navigate("/modelAttach?projectName=" + data.projectName, true);
                    } else if (menudata.id === 'task') {
                        parent.layui.febs.navigate("/task?projectName=" + data.projectName, true);
                    } else if (menudata.id === 'schedule') {
                        parent.layui.febs.navigate("/schedule?projectCode=" + data.projectCode, true);
                    } else if (menudata.id === 'progress') {
                        parent.layui.febs.navigate("/progress?projectCode=" + data.projectCode, true);
                    } else if (menudata.id === 'edit') {
                        parent.layui.febs.navigate('/sypqProject/update/' + data.projectCode, true);
                    } else if (menudata.id === 'audit') {
                        parent.layui.febs.navigate('/sypqProject/audit/' + data.projectCode, true);
                    } else if (menudata.id === 'copy') {
                        parent.layui.febs.navigate('/sypqProject/copy/' + data.projectCode, true);
                    } else if (menudata.id === 'detail') {
                        parent.layui.febs.navigate('/sypqProject/detail/' + data.projectCode, true);
                    } else if (menudata.id === 'del') {
                        console.log("layEvent === del");
                        febs.modal.confirm('删除数据', '确定删除所选数据？', function () {
                            deleteIds(data.projectCode);
                        });
                    } else if (menudata.id === 'stop') {
                        febs.modal.confirm('终止项目', '确定终止所选项目？', function () {
                            febs.get(ctx + 'sypqProject/stop/' + data.projectCode, null, function () {
                                febs.alert.success('项目终止成功');
                                $query.trigger('click');
                            });
                        });
                    }
                },
                align: 'right', // 右对齐弹出
                style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' // 设置额外样式
            });
        }

    });

    table.on('toolbar(sypqProjectTable)', function (obj) {
        if (obj.event === 'add') {
            parent.layui.febs.navigate("/sypqProject/add", true);
        }
        if (obj.event === 'export') {
            febs.download(ctx + 'sypqProject/excel', getQueryParams(), '项目列表.xlsx');
        }
    });

    function getQueryParams() {
        return {
            projectName: $searchForm.find('input[name="projectName"]').val() == null ? "" : $searchForm.find('input[name="projectName"]').val().trim(),
            custName: $searchForm.find('input[name="custName"]').val() == null ? "" : $searchForm.find('input[name="custName"]').val().trim(),
            managers: $searchForm.find('input[name="managers"]').val() == null ? "" : $searchForm.find('input[name="managers"]').val().trim(),
            approveStatus: $approveStatus.val() == null ? "" : $approveStatus.val().trim(),
            status: $status.val() == null ? "" : $status.val().trim(),
            invalidate_ie_cache: new Date()
        };
    }

    function deleteIds(ids) {
        febs.get(ctx + 'sypqProject/delete/' + ids, null, function () {
            febs.alert.success('删除数据成功');
            $query.trigger('click');
        });
    }

    function getTableHeight() {
        return commonJS.calcTableHeight();
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });

});
